// AddressCombo.cpp : implementation file
//

#include "stdafx.h"
#include "XIE.h"
#include "AddressCombo.h"
#include "MainFrm.h"


// CAddressCombo

IMPLEMENT_DYNAMIC(CAddressCombo, CComboBoxEx)

CAddressCombo::CAddressCombo()
{

}

CAddressCombo::~CAddressCombo()
{
}


BEGIN_MESSAGE_MAP(CAddressCombo, CComboBoxEx)
	ON_WM_SETFOCUS()
	ON_WM_CREATE()
END_MESSAGE_MAP()



// CAddressCombo message handlers



void CAddressCombo::OnSetFocus(CWnd* pOldWnd)
{
	CComboBoxEx::OnSetFocus(pOldWnd);

	// TODO: Add your message handler code here	
}

int CAddressCombo::Create(CWnd* pWndParent, int nID)
{
	return CComboBoxEx::Create(CBS_DROPDOWN | CBS_AUTOHSCROLL | WS_CHILD | WS_VISIBLE, 
		CRect(0, 0, 200, 450), pWndParent, nID);
	
}

int CAddressCombo::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CComboBoxEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here

	CString strEntry;
	HKEY hReg;
	TCHAR chData[MAX_PATH];
	DWORD dwType = REG_SZ;

	typedef HRESULT (CALLBACK* LPFNDLLFUNC1)(HWND ,DWORD);


	SetExtendedStyle(0, GetExtendedStyle()|CBES_EX_NOSIZELIMIT);

	HINSTANCE hIns = LoadLibrary(_T("shlwapi.dll"));
	if(hIns != NULL)
	{
		LPFNDLLFUNC1 lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hIns, "SHAutoComplete");
		if(lpfnDllFunc1!=NULL)
			lpfnDllFunc1(GetEditCtrl()->m_hWnd, 0xe);

		FreeLibrary(hIns);
	}


	ResetContent();
	LimitText(0);
	InitImage();

	if (RegOpenKeyEx(HKEY_CURRENT_USER, 
		_T("Software\\Microsoft\\Internet Explorer\\TypedUrls"), 0, KEY_READ, &hReg) == ERROR_SUCCESS)
	{

		for (int iMRU = 0; ;iMRU++ )
		{
			strEntry.Format(_T("url%i"), iMRU + 1);

			DWORD dwData = MAX_PATH - 1;
			if (RegQueryValueEx(hReg, strEntry, NULL, &dwType , (LPBYTE)&chData, &dwData) == ERROR_SUCCESS)
			{
				/*
				COMBOBOXEXITEM   Item; 
				Item.mask   =   CBEIF_TEXT   ; 
				Item.pszText   =   (LPTSTR)chData; 
				Item.iItem   =   GetCount();//index 
				*/
				Insert((LPCTSTR)chData, GetCount());
			} 
			else break;
		}

		RegCloseKey(hReg);
	}

	return 0;
}

void CAddressCombo::InitImage() 
{
	CImageList *oldimg;
	oldimg = GetImageList();
	if(oldimg != NULL)
		oldimg->DeleteImageList();

	CBitmap bm;
	
	bm.LoadBitmap(IDB_FAV_BAR);
	
	DWORD dwStyle = GetExtendedStyle();
	dwStyle &= ~(CBES_EX_NOEDITIMAGE|CBES_EX_NOEDITIMAGEINDENT) ;
	SetExtendedStyle(0,dwStyle);
	CImageList img;
	img.Create(16, 16, ILC_COLORDDB|ILC_MASK, 2, 1);

	ImageList_AddMasked(img.GetSafeHandle(), (HBITMAP)bm.GetSafeHandle(), RGB(255,0,255));

	bm.DeleteObject();

	SetImageList(&img);

	img.Detach();
}

int CAddressCombo::Insert(LPCTSTR lpsztext, int nIndex)
{
	COMBOBOXEXITEM item;

	item.mask = CBEIF_TEXT|CBEIF_IMAGE|CBEIF_SELECTEDIMAGE ;
	item.iItem = nIndex;	
	item.pszText = (LPTSTR)lpsztext;
	item.iImage = 1;
	item.iSelectedImage = 1;
	InsertItem(&item);
	return 0;
}

void CAddressCombo::SetAddress(LPCTSTR lpszUrl)
{
	// This is called when the browser has completely loaded the new location,
	// so make sure the text in the address bar is up to date and stop the
	try{
		SetWindowText(lpszUrl);
		COMBOBOXEXITEM item;
		item.mask = CBEIF_IMAGE|CBEIF_SELECTEDIMAGE ;
		item.iItem = -1;
		item.iImage = 1;
		item.iSelectedImage = 1;
		SetItem(&item);
	}catch(...){}
}